﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CarbonExchange.Bll
{
    public class ContentNode : SequenceItemBase
    {
        internal ContentNode()
        {
        }

        public int Id { get; set; }
        public int SiteId { get; set; }
        public string SiteName { get; set; }
        public Guid NodeGuid { get; internal set; }
        public Guid? ParentGuid { get; set; }
        public string ParentPath { get; internal set; }
        public string ParentSequence { get; internal set; }
        public string Name { get; set; }
        public string DisplayName { get; set; }
        public int TemplateId { get; set; }
        public string TemplateName { get; internal set; }
        //public int? ThumbnailId { get; set; }
        public string Summary { get; set; }
        public DateTime CreatedDateTime { get; internal set; }
        public DateTime? PublishFrom { get; internal set; }
        public DateTime? PublishTo { get; internal set; }
        public bool RequiresPublishing { get; internal set; }
        public bool ReadyToPublish { get; internal set; }
        public string Notes { get; set; }
        public DateTime? CurrentVersion { get; internal set; }
        public bool DeveloperLocked { get; set; }

        internal Dal.Context Context { get; set; }

        private IQueryable<ContentNode> children;
        public virtual IQueryable<ContentNode> Children
        {
            get
            {
                if (children == null)
                {
                    children = GetChildren(CurrentVersion);
                }
                return children;
            }
        }


        public IQueryable<ContentNode> GetChildren(DateTime? version)
        {
            if (Context != null)
            {
                ContentNodeFactory factory = new ContentNodeFactory(Context);
                return factory.GetAllByParentGuid(this.NodeGuid, version);
            }
            else
            {
                return null;
            }
        }

        public ContentNode FindChildByName(string name)
        {
            if (string.IsNullOrEmpty(name))
                return null;
            var tmp = Children.ToList();
            if(tmp != null)
                return tmp.Where(x => x.Name.Equals(name, StringComparison.InvariantCulture)).SingleOrDefault();
            else
                return null;
        }

        public TemplateFile GetDefaultFile()
        {
            if (TemplateFiles != null)
            {
                return TemplateFiles.Where(x => x.IsDefault).SingleOrDefault().Convert();
            }
            else
            {
                return null;
            }
        }

        internal LinqUtilities.LazyList<Dal.TemplateFile> TemplateFiles;
        
        private Dictionary<string, TemplateFile> files;
        public TemplateFile GetFile(string name)
        {
            if (TemplateFiles != null)
            {
                if (files == null)
                {
                    files = new Dictionary<string, TemplateFile>();
                }
                if (!files.ContainsKey(name))
                {
                    files.Add(name, TemplateFiles.Where(x => x.Name == name).SingleOrDefault().Convert());
                }
                return files[name];
            }
            else
            {
                return null;
            }
        }

        public string GetDefaultQueryString()
        {
            return GetQueryString(GetDefaultFile());
        }

        public string GetQueryString(string name)
        {
            return GetQueryString(GetFile(name));
        }

        public string GetQueryString(TemplateFile file)
        {
            if (file != null)
            {
                System.Text.StringBuilder queryString = new StringBuilder();
                queryString.AppendFormat("AcmsPostId={0}", Id);
                queryString.AppendFormat("&AcmsSiteId={0}", SiteId);
                if (!string.IsNullOrEmpty(file.MasterPagePath))
                    queryString.AppendFormat("&AcmsMasterPagePath={0}", file.MasterPagePath);
                if (!string.IsNullOrEmpty(file.Path))
                    queryString.AppendFormat("&AcmsTemplatePath={0}", file.Path);
                if (!string.IsNullOrEmpty(file.Theme))
                    queryString.AppendFormat("&AcmsTheme={0}", file.Theme);
                return queryString.ToString();
            }
            else
            {
                return null;
            }
        }

        public string Path
        {
            get { return string.Concat(ParentPath, Name); }
        }

        internal CarbonExchange.LinqUtilities.LazyItem<DateTime, CarbonExchange.Dal.ContentNodePosition> LiveVersion;

        public string Url
        {
            get
            {
                StringBuilder url = new StringBuilder();
                if (this.Id > 0 && this.Path != "~")
                {
                    url.AppendFormat("{0}{1}.aspx", ParentPath, this.Name);
                }
                else if (this.Id > 0 && this.Path == "~")
                {
                    url.Append("~/default.aspx");
                }
                else
                {
                    return null;
                }

                string qs = null;
                if (CurrentVersion == null)
                {
                    qs = "AcmsSV=Pending";
                }
                else if (CurrentVersion.Value != LiveVersion.Item)
                {
                    qs = string.Concat("AcmsSV=", System.Web.HttpUtility.UrlEncode(CurrentVersion.Value.ToString("yyyy/MM/dd HH:mm")));
                }
                if (qs != null)
                {
                    url.Append("?");
                    url.Append(qs);
                }
                return url.ToString();
            }
        }

    }
}
